import {
  computedWH,
  drawMyQrcode,
  handlerGobackClick,
  hint,
  onlyChangeRouter,
} from "../../../utils/util";
import { Subscribe } from "../../../models/subscribe";

const subscribe = new Subscribe();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: [true, false],
    isVisitor: true,
    activeTab: 0,
    activeTab1: undefined,
    tabOne0: [],
    tabOne1: [],
    tabOne2: [],
    //审核
    tabTwo0: [],
    tabTwo1: [],
    tabTwo2: [],
    scrollH: "auto",
    //加载中状态
    loading: false,
    loading1: false,
    loading2: false,
    loading3: false,
    loading4: false,
    loading5: false,
    //没有更多了
    noMore: false,
    noMore1: false,
    noMore2: false,
    noMore3: false,
    noMore4: false,
    noMore5: false,
    //加载失败
    loadingFailed: false,
    loadingFailed1: false,
    loadingFailed2: false,
    loadingFailed3: false,
    loadingFailed4: false,
    loadingFailed5: false,
    //当前已加载页数
    pageNo: 1,
    pageNo1: 1,
    pageNo2: 1,
    pageNo3: 1,
    pageNo4: 1,
    pageNo5: 1,
    isShowRed: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var query = wx.createSelectorQuery();
    query
      .select("#scrollH")
      .boundingClientRect((res) => {
        console.log(res);
        let height = res.height - 44;
        this.setData({
          scrollH: height,
        });
      })
      .exec();
  },
  onShow: function () {
    this.initPageData();
  },
  initPageData() {
    subscribe
      .subscribeList()
      .then((res) => {
        this.setData({
          tabOne0: res.result.records,
        });
      })
      .catch((err) => {
        console.log(err);
      });
    subscribe
      .subscribeList(1, 10, 1)
      .then((res) => {
        this.setData({
          tabOne1: res.result.records,
        });
      })
      .catch((err) => {
        console.log(err);
      });
    subscribe
      .subscribeList(1, 10, 2)
      .then((res) => {
        this.setData({
          tabOne2: res.result.records,
        });
      })
      .catch((err) => {
        console.log(err);
      });
    subscribe
      .auditList()
      .then((res) => {
        this.setData({
          tabTwo0: res.result.records,
        });
      })
      .catch((err) => {
        console.log(err);
      });
    subscribe
      .auditList(1, 10, 1)
      .then((res) => {
        this.setData({
          tabTwo1: res.result.records,
        });
      })
      .catch((err) => {
        console.log(err);
      });
    subscribe
      .auditList(1, 10, 2)
      .then((res) => {
        this.setData({
          tabTwo2: res.result.records,
        });
      })
      .catch((err) => {
        console.log(err);
      });
    subscribe
      .subscribeGetRed()
      .then((res) => {
        this.setData({
          isShowRed: res.result,
        });
      })
      .catch((err) => {
        console.log(err);
      });
  },
  changeItme(event) {
    var index = event.currentTarget.dataset["index"];
    var active = [false, false];
    active[index] = true;
    this.setData({
      active,
      activeTab1: 0,
    });
  },
  onChange(event) {
    // wx.showToast({
    //   title: `切换到标签 ${event.detail.name}`,
    //   icon: 'none',
    // });
  },
  onlyChangeRouter,
  goHome() {
    wx.switchTab({
      url: "/pages/home/index",
    });
  },
  goQrcodeDetail(e) {
    let status = e.currentTarget.dataset.status;
    let commid = e.currentTarget.dataset.commid;
    let phone = e.currentTarget.dataset.ownerphone;
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/owner-app/pages/visitor-qrcode/index?commid=${commid}&phone=${phone}&id=${id}`,
    });
  },
  goAuditDetail(e) {
    let status = e.currentTarget.dataset.status;
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/owner-app/pages/audit-detail/index?id=" + id,
    });
  },
  scrollBottom() {
    if (!this.data.loading && !this.data.noMore) {
      this.setData({
        loading: true,
        pageNo: this.data.pageNo + 1,
      });
      this.getData(
        "subscribeList",
        "loading",
        "tabOne0",
        "pageNo",
        "loadingFailed",
        0,
        "noMore",
        true
      );
    }
  },
  scrollBottom1() {
    if (!this.data.loading1 && !this.data.noMore1) {
      this.setData({
        loading1: true,
        pageNo1: this.data.pageNo1 + 1,
      });
      this.getData(
        "subscribeList",
        "loading1",
        "tabOne1",
        "pageNo1",
        "loadingFailed1",
        1,
        "noMore1",
        true
      );
    }
  },
  scrollBottom2() {
    if (!this.data.loading2 && !this.data.noMore2) {
      this.setData({
        loading2: true,
        pageNo2: this.data.pageNo2 + 1,
      });
      this.getData(
        "subscribeList",
        "loading2",
        "tabOne2",
        "pageNo2",
        "loadingFailed2",
        2,
        "noMore2",
        true
      );
    }
  },
  scrollBottom3() {
    if (!this.data.loading3 && !this.data.noMore3) {
      this.setData({
        loading3: true,
        pageNo3: this.data.pageNo3 + 1,
      });
      this.getData(
        "auditList",
        "loading3",
        "tabTwo0",
        "pageNo3",
        "loadingFailed3",
        0,
        "noMore3",
        true
      );
    }
  },
  scrollBottom4() {
    if (!this.data.loading4 && !this.data.noMore4) {
      this.setData({
        loading4: true,
        pageNo4: this.data.pageNo4 + 1,
      });
      this.getData(
        "auditList",
        "loading4",
        "tabTwo1",
        "pageNo4",
        "loadingFailed4",
        1,
        "noMore4",
        true
      );
    }
  },
  scrollBottom5() {
    if (!this.data.loading5 && !this.data.noMore5) {
      this.setData({
        loading5: true,
        pageNo5: this.data.pageNo5 + 1,
      });
      this.getData(
        "auditList",
        "loading5",
        "tabTwo2",
        "pageNo5",
        "loadingFailed5",
        2,
        "noMore5",
        true
      );
    }
  },
  /**
   *
   * @param methodName   同类下面的方法名称
   * @param loadingName  加载状态变量
   * @param dataName     data中设定的数据
   * @param isPage       是否是分页查询
   */
  getData(
    methodName,
    loadingName,
    dataName,
    pageNoName,
    loadingFailedName,
    type = 0,
    noMoreName,
    isPage
  ) {
    subscribe[methodName](this.data[pageNoName], 10, type)
      .then((res) => {
        this.setData({
          [loadingName]: false,
        });
        //有分页
        if (isPage) {
          this.setData({
            [dataName]: this.data[dataName].concat(res.result.records),
          });
        }
        //没有分页就直接赋值
        else {
          this.setData({
            [dataName]: res.result.records,
          });
        }
        if (res.result.records.length == 0) {
          this.setData({
            [noMoreName]: true,
          });
        }
      })
      .catch((err) => {
        this.setData({
          [loadingFailedName]: true,
        });
      });
  },
});
